package com.vtb.base;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vtb.dao.impl.QqDaoImpl;
import com.vtb.dao.impl.RrDaoImpl;
import com.vtb.dao.impl.SsDaoImpl;
import com.vtb.entity.DataCache;
import com.vtb.entity.QqOauth;
import com.vtb.entity.RrOauth;
import com.vtb.entity.SsOauth;
import com.vtb.entity.Var;

/**
 * VtbBaseServlet
 * 
 * @author Chen Tao
 * @date 2012-7-1 上午08:39:08
 */
@SuppressWarnings("serial")
public class VtbBaseServlet extends HttpServlet {
    private static final Log log = LogFactory.getLog(VtbBaseServlet.class);
    protected HttpSession session;
    protected HashMap<String, Object> lastMap;
    protected VtbDaoFactory daoFactory = VtbDaoFactory.getInstance();
    protected RrDaoImpl rrDao = (RrDaoImpl) daoFactory.getDao(Var.RRDAO);
    protected QqDaoImpl qqDao = (QqDaoImpl) daoFactory.getDao(Var.QQDAO);
    protected SsDaoImpl ssDao = (SsDaoImpl) daoFactory.getDao(Var.SSDAO);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        super.doPost(request, response);
    }
    
    /**
     * 授权后Session信息处理
     * 没有lastMap则创建
     * 如果有 在长度为3的时候清空
     *
     * @param request
     * @param response
     * @author Chen Tao
     * @date 2012-7-4 上午9:39:02
     */
    @SuppressWarnings("unchecked")
    protected void doSession(HttpServletRequest request, HttpServletResponse response){
        
        HttpSession session = request.getSession();
        HashMap<String, Object> lastMap = (HashMap<String, Object>) session.getAttribute("lastMap");
        if (lastMap == null) {
            lastMap = new HashMap<String, Object>();
        }
        if (lastMap.size() == 3) {
            lastMap.clear();
        }
    }
    
    
    /**
     * 更新人人授权内容
     *
     * @param rrOauth
     * @param response
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-4 上午9:19:58
     */
    protected RrOauth updateRr(RrOauth rrOauth){
        RrOauth oldrrOauth = DataCache.rrmap.get(rrOauth.getUni());
        rrOauth.setRefq(oldrrOauth.getRefq());
        rrOauth.setRefs(oldrrOauth.getRefs());
        rrOauth.setR2s(oldrrOauth.getR2s());
        rrOauth.setR2q(oldrrOauth.getR2q());
        try {
            rrDao.update(rrOauth);
            DataCache.synPutMap(rrOauth);
        } catch (Exception e) {
            log.error("修改人人信息错误：", e);
            return null;
        }
        return rrOauth;
    }
    /**
     * 更新qq授权内容
     *
     * @param qqOauth
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-4 上午9:31:07
     */
    protected QqOauth updateQq(QqOauth qqOauth){
        QqOauth oldqqOauth = DataCache.qqmap.get(qqOauth.getUni());
        qqOauth.setRefr(oldqqOauth.getRefr());
        qqOauth.setRefs(oldqqOauth.getRefs());
        qqOauth.setQ2r(oldqqOauth.getQ2r());
        qqOauth.setQ2s(oldqqOauth.getQ2s());
        try {
            qqDao.update(qqOauth);
            DataCache.synPutMap(qqOauth);
        } catch (Exception e) {
            log.error("修改qq信息错误：", e);
            return null;
        }
        return qqOauth;
    }
    /**
     * 更新新浪授权内容
     *
     * @param qqOauth
     * @return
     * @throws IOException
     * @author Chen Tao
     * @date 2012-7-4 上午9:31:07
     */
    protected SsOauth updateSs(SsOauth ssOauth){
        SsOauth oldsSsOauth = DataCache.ssmap.get(ssOauth.getUni());
        ssOauth.setRefq(oldsSsOauth.getRefr());
        ssOauth.setRefr(oldsSsOauth.getRefr());
        ssOauth.setS2r(oldsSsOauth.getS2r());
        ssOauth.setS2q(oldsSsOauth.getS2q());
        try {
            ssDao.update(ssOauth);
            DataCache.synPutMap(ssOauth);
        } catch (Exception e) {
            log.error("修改qq信息错误：", e);
            return null;
        }
        return ssOauth;
    }
}
